package main

import (
	"context"
	"fmt"
	"runtime"
	"sync"
	"time"
	"unsafe"
)

func deadloop() {
	i := 0
	for {
		i++
		//runtime.Gosched()
	}
}

func worker() {
	for {
		fmt.Println("worker is running")
		time.Sleep(time.Second * 1)
	}
}

func worker1(c context.Context, wg *sync.WaitGroup, id int) {
	defer wg.Done()

	select {
	case <-time.After(time.Second):
		fmt.Sprintln("执行完成 %", id)
	case <-c.Done():
		fmt.Sprintln("请求超时 %", id)
	}

	fmt.Println("okkkk")
}

func worker2() {
	defer fmt.Println("Exiting goroutine.")

	for i := 0; i < 5; i++ {
		fmt.Println("Running.")
		time.Sleep(1 * time.Second)
	}

	runtime.Goexit()

	fmt.Println("This line will not be printed.")
}

func worker3(ctx context.Context) {
	for {
		select {
		case <-ctx.Done():
			fmt.Println("Exiting goroutine.")
			return
		default:
			fmt.Println("Still running.")
			time.Sleep(1 * time.Second)
		}
	}
}

func main() {
	ctx3, cancel := context.WithTimeout(context.Background(), 5*time.Second)
	defer cancel()

	go worker3(ctx3)
	time.Sleep(10 * time.Second)
	fmt.Println("Exiting main.")
	return

	go worker2()

	time.Sleep(10 * time.Second)
	fmt.Println("Exiting main.")

	return
	var wg sync.WaitGroup
	wg.Add(1)
	select {
	case <-time.After(time.Second):
		time.Sleep(time.Second * 2)
		fmt.Println("oksss")

		wg.Done()
	}

	fmt.Println("oksss111")
	wg.Wait()
	return
	fmt.Println("default gomax:", runtime.GOMAXPROCS(2))
	fmt.Println("cur gomax:", runtime.GOMAXPROCS(0))

	go func() {
		fmt.Println("hello world")
	}()

	fmt.Println("size:", unsafe.Sizeof(int64(1)))

	go func() {
		for {
			_ = make([]int64, 8192)
		}
	}()
	fmt.Println("end")
	select {}

}
